<template>
  <div class="allsearch">
    <section class="search-content">
      <el-row :gutter="4" type="flex" justify="start">
        <el-col :span="24">
          <el-input v-model="username" style="width:200px" placeholder="用户名" @keyup.enter.native="handleFilter"/>
          <span>领取时间：</span>
          <el-date-picker
            v-model="cycle"
            :picker-options="picker"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="|"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            style="width:400px"
            @keyup.enter.native="handleFilter"
          />
          <el-select v-model="award" placeholder="奖励分类" style="width:150px;" @change="handleFilter">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <el-button type="primary" size="medium" @click="handleFilter">搜索</el-button>
          <el-button size="medium" type="info" @click="clearParams">清空</el-button>
        </el-col>
      </el-row>
    </section>
    <subtable
      :table-label="inviteLabel"
      :table-data="listFilter.slice((currentpage-1)*pagesize,currentpage*pagesize)"
    />
    <pagination
      :total="total"
      :current-page="currentpage"
      :page-size="pagesize"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
  </div>
</template>

<script>
import Subtable from '@/components/Eltable'
import Pagination from '@/components/Pagination' // 分页组件
import { getRewardRecord } from '@/api/operate/invitation.js'
import {getTenantId} from "@/utils/auth"; // 数据接口
export default {
  components: {
    Subtable,
    Pagination
  },
  data() {
    return {
      username: '',
      cycle: [],
      total: 0,
      currentpage: 1,
      pagesize: 10,
      listFilter: [],
      award: '',
      picker: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      options: [
        {
          value: '',
          label: '奖励方式'
        },
        {
          value: '注册奖励',
          label: '注册奖励'
        },
        {
          value: '邀请奖励',
          label: '邀请奖励'
        }
      ],
      inviteLabel: [
        {
          label: '用户名', width: '', prop: 'userName'
        }, {
          label: '领取时间', width: '', prop: 'startTimeStr'
        }, {
          label: '面值', width: '', prop: 'faceValue'
        }, {
          label: '使用情况', width: '', prop: 'status'
        }, {
          label: '使用时间', width: '', prop: 'endTimeStr'
        }, {
          label: '订单信息', width: '', prop: 'orderName'
        }, {
          label: '奖励方式', width: '', prop: 'typeStr'
        }
      ],
      inviteData: []
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    handleSizeChange(val) {
      this.currentpage = 1
      this.pagesize = val.value
      if (this.pagesize >= 20) {
        scrollTo(50, 800)
      }
    },
    handleCurrentChange(val) {
      this.currentpage = val.value
      if (this.pagesize >= 20) {
        scrollTo(50, 800)
      }
    },
    handleFilter() {
      this.listFilter = this.inviteData.filter(item => {
        if (this.username && item.userName.toLowerCase().indexOf(this.username.toLowerCase()) === -1) return false
        if (this.cycle && (new Date(item.startTime) < this.cycle[0] || new Date(item.startTime) > this.cycle[1])) return false
        if (this.award && item.typeStr !== this.award) return false
        return true
      })
      this.total = this.listFilter.length
    },
    clearParams() {
      this.cycle = []
      this.username = ''
      this.award = ''
      this.getData()
    },
    getData() {
      getRewardRecord().then(response => {
        this.inviteData = response.data.items.filter(item => {
          if (item.tenantId !== getTenantId()) return false
          return true
        })
        this.inviteData.map(item => {
          if (item.startTime) item.startTimeStr = new Date(item.startTime).toLocaleString().replace(/[\u4e00-\u9fa5]/g, '')
          if (item.endTime) item.endTimeStr = new Date(item.endTime).toLocaleString().replace(/[\u4e00-\u9fa5]/g, '')
          if (item.type === 'register') item.typeStr = '注册奖励'
          if (item.type === 'invitation') item.typeStr = '邀请奖励'
        })
        this.total = this.inviteData.length
        this.listFilter = this.inviteData
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.allsearch {
  margin-top: 0px;
  padding: 0;
  .search-content {
    width: 100%;
  }
}
</style>
